package com.abc.controller;

import com.abc.bean.Purchase;
import com.abc.bean.Stock;
import com.abc.service.PurchaseService;
import com.abc.service.StockFeignService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/*
 * @Author yaxiongliu
 **/
@RestController
public class PurchaseController {


    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private StockFeignService stockFeignService;

    @GlobalTransactional(timeoutMills = 60000*5)
    @PostMapping("/purchase/add/{deno}")
    public boolean addPurchaseHandle(@RequestBody Purchase purchase
            ,@PathVariable("deno") int deno) {
        purchaseService.addPurchase(purchase);

        Stock stock = new Stock();
        stock.setName(purchase.getName());
        stock.setTotal(purchase.getCount());

        Boolean result= stockFeignService.saveStock(stock);

        //服务降级后，抛出异常回滚全局事务
        if(!result){
            throw new RuntimeException();
        }

        int i=1/deno;

        if (result != null) {

            return result;
        }
        return false;
    }



}
